﻿// 文件名：递归
// 创建时间：2021/6/21/星期一 21:01:28

#include <iostream>
using namespace std;

//一个递归函数调用的例子
void countdown(int n);
void recur()
{
	countdown(4);
}
void countdown(int n)
{
	cout << "Counting down ... " << n<<" (n at "<<&n<<" )" << endl;
	if (n > 0)
	{
		countdown(n - 1);
	}
	cout << n << ": Kaboom" << " (n at " << &n << " )\n";
}

//利用多个递归调用实现平分标尺
const int len = 66;
const int divs = 6;
void subdivide(char* str, int low, int high, int level);
void ruler()
{
	char ruler[len];
	for (int i = 1; i < len - 2; i++) //字符串总长度为len,最后一个字符为空字符
	{
		ruler[i] = ' ';
	}
	ruler[len - 1] = '\0';
	int max = len - 2;
	int min = 0;
	ruler[min] = ruler[max] = '|';
	cout << ruler << endl;
	for (int j = 1; j <= divs; j++)
	{
		subdivide(ruler, min, max, j);
		cout << ruler << endl;
		/*for (int k = 1; k < len - 2; k++)
		{
			ruler[k] = ' ';
		}*/
	}
}

void subdivide(char* str, int low, int high, int level)
{
	if (level == 0)
	{
		return;
	}
	int mid = (high + low) / 2;
	str[mid] = '|';
	subdivide(str, low, mid, level - 1); // 递归调用实现平分左半边
	subdivide(str, mid, high, level - 1);// 递归调用实现平分右半边
}

int main()
{
	//recur()调用递归函数countdown()
	//recur();

	//平分标尺
	ruler();

	system("pause");
	return 0;
}